﻿using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using Microsoft.VisualStudio.SharePoint;

namespace TomVanGaever.SharePoint.VS2010.BCSGenerator.Logic
{
    [Export(typeof(ISharePointProjectExtension))]
    class ProjectExtension : ISharePointProjectExtension
    {
        private const string TITLE = "Generate BCS Model";
        private ISharePointProject m_project;
        private ISharePointProjectLogger m_log;

        public void Initialize(ISharePointProjectService projectService)
        {
            // Initiate logger object
            m_log = projectService.Logger;

            // Associate custom modules in Visual Studio 2010
            // Context menu on project
            projectService.ProjectMenuItemsRequested += new EventHandler<SharePointProjectMenuItemsRequestedEventArgs>(projectService_ProjectMenuItemsRequested);
            // propery on project
            projectService.ProjectPropertiesRequested += new EventHandler<SharePointProjectPropertiesRequestedEventArgs>(projectService_ProjectPropertiesRequested);
        }

        private void projectService_ProjectMenuItemsRequested(object sender, SharePointProjectMenuItemsRequestedEventArgs e)
        {
            m_project = e.Project;

            // Check if the project contains a BDC file
            bool isBDCPRoject = IsBDCProject();
            if (!isBDCPRoject)
                return;

            // Add item to context menu
            IMenuItem item = e.ActionMenuItems.Add(TITLE);
            item.Click += new EventHandler<MenuItemEventArgs>(item_Click);
        }

        private void projectService_ProjectPropertiesRequested(object sender, SharePointProjectPropertiesRequestedEventArgs e)
        {
            e.PropertySources.Add((object)new BCSGeneratorProjectPropertyImagesMappedFolderProperty(e.Project));
        }

        private void item_Click(object sender, MenuItemEventArgs e)
        {
            try
            {
                string path = BCSGeneratorProjectPropertyImagesMappedFolderProperty.BCSBuilderPath;
                m_log.ActivateOutputWindow();
                string command = string.Format(path, Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86));
                m_log.WriteLine("Command: " + command, LogCategory.Message);
                string arguments = string.Format("-path \"{0}\"", m_project.OutputFullPath);
                m_log.WriteLine("Arguments: " + arguments, LogCategory.Message);
                Process.Start(command, arguments);
                m_log.WriteLine("BCS Builder started", LogCategory.Message);
            }
            catch (Exception ex)
            {
                m_log.WriteLine(ex.Message, LogCategory.Error);
            }
        }

        private bool IsBDCProject()
        {
            foreach (var projectItem in m_project.ProjectItems)
            {
                if (projectItem.ProjectItemType.Id == "Microsoft.VisualStudio.SharePoint.BusinessDataConnectivity")
                    return true;
            }
            return false;
        }

    }
}
